//
//  MKDatePickerViewController.swift
//  MKDatePickerController
//
//  Created by 劢克科技 on 2019/5/17.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit
import SnapKit

protocol MKDatePickerViewControllerDelegate {
    func handleGetDate(date: Date)
}

class MKDatePickerViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        modalPresentationStyle = .custom
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //MARK: - Getter
    
    var delegate: MKDatePickerViewControllerDelegate?
    
    lazy var datePicker: UIDatePicker = {
        let picker = UIDatePicker()
        picker.datePickerMode = .date
        picker.locale = Locale(identifier: "zh_Hans_CN")
        return picker
    }()
    
    lazy var cover: UIView = {
        let view = UIView()
        view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.1986960827)
        view.alpha = 0
        return view
    }()
    
    lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        return view
    }()
    
    lazy var cancelButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("取消", for: .normal)
        button.addTarget(self, action: #selector(handleCancel), for: .touchUpInside)
        return button
    }()
    
    lazy var submitButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("确认", for: .normal)
        button.addTarget(self, action: #selector(handleSubmit), for: .touchUpInside)
        return button
    }()
    
    //MARK: - Events
    
    // 开场动画
    fileprivate func startAnimation() {
        cover.alpha = 0
        containerView.frame = getPickerHideFrame()
        
        UIView.animate(withDuration: 0.25) {
            self.cover.alpha = 1
            self.containerView.frame = self.getPickerInitFrame()
        }
    }
    
    // 取消
    @objc fileprivate func handleCancel() {
        UIView.animate(withDuration: 0.25, animations: {
            self.cover.alpha = 1
            self.containerView.frame = self.getPickerHideFrame()
        }) { (_) in
            self.dismiss(animated: false, completion: nil)
        }
    }
    
    // 确认
    @objc fileprivate func handleSubmit() {
        UIView.animate(withDuration: 0.25, animations: {
            self.cover.alpha = 1
            self.containerView.frame = self.getPickerHideFrame()
        }) { (_) in
            self.dismiss(animated: false, completion: {
                self.delegate?.handleGetDate(date: self.datePicker.date)
            })
        }
    }
}

//MARK: - setupUI

extension MKDatePickerViewController {
    fileprivate func setupUI() {
        addSubs()
        layoutSubs()
    }
    
    fileprivate func addSubs() {
        view.addSubview(cover)
        view.addSubview(containerView)
        containerView.addSubview(datePicker)
        containerView.addSubview(cancelButton)
        containerView.addSubview(submitButton)
    }
    
    fileprivate func layoutSubs() {
        cover.frame = view.bounds
        
        containerView.frame = getPickerHideFrame()
        
        datePicker.snp.makeConstraints { (make) in
            make.left.equalTo(containerView.snp_left)
            make.right.equalTo(containerView.snp_right)
            make.bottom.equalTo(containerView.snp_bottom)
            make.height.equalTo(containerView.frame.height - 40)
        }
        
        cancelButton.snp.makeConstraints { (make) in
            make.left.equalTo(containerView.snp_left)
            make.top.equalTo(containerView.snp_top)
            make.height.equalTo(40)
            make.width.equalTo(60)
        }
        
        submitButton.snp.makeConstraints { (make) in
            make.right.equalTo(containerView.snp_right)
            make.top.equalTo(containerView.snp_top)
            make.height.equalTo(40)
            make.width.equalTo(60)
        }
    }
    
    fileprivate func getPickerInitFrame() -> CGRect {
        let pickerW = UIScreen.main.bounds.width
        let pickerH = datePicker.frame.height + 40
        let pickerX: CGFloat = 0
        let pickerY = UIScreen.main.bounds.height - pickerH
        return CGRect(x: pickerX, y: pickerY, width: pickerW, height: pickerH)
    }
    
    fileprivate func getPickerHideFrame() -> CGRect {
        let pickerW = UIScreen.main.bounds.width
        let pickerH = datePicker.frame.height + 40
        let pickerX: CGFloat = 0
        let pickerY = UIScreen.main.bounds.height
        return CGRect(x: pickerX, y: pickerY, width: pickerW, height: pickerH)
    }
}
